﻿
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using Ymt.Repository;
using YmtRpc.Domain;
using YmtRpc.Domain.Enum;


namespace YmtRpc.Web.Admin.Controllers
{
    /// <summary>
    /// 服务端的配置查询、修改
    /// </summary>
    public class ConfigController : Controller
    {

        public ActionResult ClientConfig(string appId)
        {
            ViewBag.AppList = RepositoryHub.RpcClientconfigRepository.GetAppList();
            ViewBag.AppId = appId;

            List<RpcClientconfig> list = RepositoryHub.RpcClientconfigRepository.GetList(appId);

            return View(list);
        }

        public ActionResult ServiceConfig(string dependencyRelation, string appId)
        {
            DependencyRelationEnum dependencyRelationEnum = DependencyRelationEnum.Dependent;
            
            if (!string.IsNullOrEmpty(dependencyRelation))
            {
                dependencyRelationEnum = (DependencyRelationEnum)Convert.ToInt32(dependencyRelation);
            }

            ViewBag.dependencyRelationDict = new Dictionary<string, string>()
            {
                {"1","依赖（服务的依赖服务）"}, {"2","被依赖（服务被哪些服务依赖）"}
            };

            ViewBag.dependencyRelation = Convert.ToString((int)dependencyRelationEnum);

            ViewBag.AppList = RepositoryHub.ServiceconfigRepository.GetAppList(dependencyRelationEnum);
            ViewBag.AppId = appId;

            List<SeviceConfig> list = RepositoryHub.ServiceconfigRepository.GetList(dependencyRelationEnum, appId);

            return View(list);
        }

        [HttpGet]
        public JsonResult GetAppListFromServiceConfig()
        {
            DependencyRelationEnum dependencyRelation = (DependencyRelationEnum)Convert.ToInt32(Request.QueryString["dependencyRelation"]);
            List<string> appIdList = RepositoryHub.ServiceconfigRepository.GetAppList(dependencyRelation);

            return Json(appIdList, JsonRequestBehavior.AllowGet);
        }
    }
}
